﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class ggPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
        viewIsNoData();
    }

    //获取已有公告数据
    private void BindData()
    {
        gvGG.DataSource = gg_BLL.GetGGSource();
        gvGG.DataBind();
    }

    #region 脚本
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvGG.Rows.Count == 0)
        {
            RegisterStartupScript("prompt", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未找到相关数据)'</script>");
        }
    }
    /// <summary>
    /// 刷新当前页
    /// </summary>
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript'>location.replace(location.href)</script>");
    }

    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion

    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtTitle.Text == "" || txtCont.Text == "")
        {
            ShowMessage("数据信息不完整！");
            return;
        }

        //创建新的公告数据对象并赋值 
        GG newGG = new GG();
        newGG.ggTitle = txtTitle.Text;
        newGG.ggCont = txtCont.Text;
        newGG.ggDate = DateTime.Now;
        //验证用户操作类型
        if (btnSubmit.Text == "添加")
        {
            //请求添加数据
            bool bl = gg_BLL.AddNewGG(newGG);
            if (bl)
            {
                ShowMessage("添加成功！");
                RefurPage();
            }
            else
                ShowMessage("添加失败！");
        }
        else
        {
            //请求更新数据 
            bool b = gg_BLL.UpdateGG(seleGGNum, newGG);
            if (b)
            {
                ShowMessage("更新成功！");
                BindData();
                btnSubmit.Text = "添加";
                btnReset_Click(sender, e);
            }
            else
                ShowMessage("更新失败！");

        }
    }

    //当选中某条公告信息时
    private static string seleGGNum = "";
    protected void gvGG_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //获取当前公告编号
        seleGGNum = gvGG.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvGG.Rows[e.NewSelectedIndex];
        //填充控件
        txtTitle.Text = row.Cells[0].Text;
        txtCont.Text = row.Cells[1].Text;
        btnSubmit.Text = "更新";
    }

    //重置按钮
    protected void btnReset_Click(object sender, EventArgs e)
    {
        btnSubmit.Text = "添加";
        txtTitle.Text = txtCont.Text = "";
    }

    //删除
    protected void gvGG_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //请求删除数据
        bool bl = gg_BLL.DeleteGG(gvGG.DataKeys[e.RowIndex][0].ToString());
        if (bl)
        {
            //重新绑定数据
            BindData();
            ShowMessage("删除成功！");
        }
        else
            ShowMessage("删除失败！");
    }

    //翻页
    protected void gvGG_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvGG.PageIndex = e.NewPageIndex;
        BindData();
    }
}
